package com.example.demo.controller;
import com.example.demo.Server.BookServer;
import com.example.demo.constant.Constants;
import com.example.demo.mapper.BookMapper;
import com.example.demo.model.*;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * Spring Ioc 使用:
 * 1: 如何把对象放到 Spring 中 : IOC  : @Component
 * 2: 如何从 Spring 中 取出对象:DI : @Autowired
 */
@RestController
@RequestMapping("/book")
@Slf4j
public class BookController {
//    @Autowired
//    BookServer bookServer ;
    @Autowired
    BookServer bookServer ;
    @RequestMapping("/getList")
    public List<BookInfo> getList(){
        List<BookInfo> bookInfos = bookServer.bookServer();

        return bookInfos;

    }
    @RequestMapping("/addBook")
    public Boolean addBook(BookInfo bookInfo){
        log.info(bookInfo.getBookName());
        log.info("BookController.addBook 接收参数: {}", bookInfo);
        //1. 参数校验
        //2. 操作数据库
        //3. 返回结果
//        if (!StringUtils.hasLength(bookInfo.getBookName())
//                || !StringUtils.hasLength(bookInfo.getAuthor())
//                || bookInfo.getCount()==null
//                || bookInfo.getPrice() == null
//                || !StringUtils.hasLength(bookInfo.getPublish())){
//            log.warn("参数不正确");
//            return false;
//        }
        bookServer.addBook(bookInfo);
        return true;

    }
    @Autowired
    private BookMapper bookMapper;
    @RequestMapping(value = "/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(RequestPage requestPage, HttpSession session){
//        Integer count = bookMapper.count();
//        Integer offset  = requestPage.offset();
//        List<BookInfo> bookInfos = bookMapper.getListByPage(offset,requestPage.getPageSize());
          UserInfo userInfo = (UserInfo) session.getAttribute(Constants.USER_SESSION_KEY);
          if(userInfo==null){
              return Result.nologin();
          }


        PageResult<BookInfo> pageResult = bookServer.getListByPage(requestPage);


        return Result.success(pageResult);




    }
    /**
     * 修改图书信息
     *  1.根据id 获取图书信息
     *  2 .修改图书信息
     */
    @RequestMapping("/getBookInfo")
  public BookInfo getBookInfo(Integer bookId){
        log.info("getBookInfo,id :{}",bookId);
        if(bookId==null){
            return new BookInfo();
        }
        return bookServer.getBookInfo(bookId);

  }

  @RequestMapping("/updateBook")
  public Boolean updateBook(BookInfo bookInfo){
        log.info("updateBook,bookInfo : {}",bookInfo);
        if(bookInfo.getId()<0){
            log.error("id:{} 不合法"+bookInfo.getId());
            return false;
        }
       Integer result = bookServer.updateBook(bookInfo);
        if(result > 0){
            return true;
        }
        return false;
    }

    /**
     * 删除图书信息
     */
    @RequestMapping("/deleteBook")
    public Boolean deleteBook(Integer bookId){
        log.info("deleteBook,bookId:{}",bookId);
        if(bookId==null){
            return false;
        }
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(bookId);
        bookInfo.setStatus(0);
        Integer result = bookServer.updateBook(bookInfo);
        if(result==0){
            return false;
        }
        return true;

    }

    /**
     * 批量删除图书
     */
    @RequestMapping("/batchDelete")

    public Boolean batchDelete(@RequestParam List<Integer> ids){
        log.info("batchDelete,ids:{}",ids);
        if(ids==null){
            return false;
        }
        try{
            Integer result = bookServer.batchDelete(ids);
            if(result>0){
                return true;
            }
            return false;
        }catch (Exception e){
            log.error("batchDelete error: " ,e);
            return false;

        }


    }





}
